package com.easemob.helpdesk.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.helpdesk.utils.DownloadManager;
import com.housekeeperapp.BuildConfig;
import com.housekeeperapp.R;
import com.net.ApiConfig;
import com.reactnativenavigation.layouts.Layout;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


import okhttp3.Request;

/**
 * Created by xiaolong.wei on 2019/7/2.
 */

public class ProgressDialogActivity extends BaseActivityV2  {
    private static final String TAG = "ProgressDialogActivity";


    @Override
    protected void initFindView() {
        progressBar=findViewById(R.id.progressBar);

        percent=findViewById(R.id.percent);
        dialogContainer=findViewById(R.id.dialog_container);

        title=findViewById(R.id.title);
        closeDialog=findViewById(R.id.close_dialog);
        topLine=findViewById(R.id.top_line);

        content=findViewById(R.id.content);
        bottomLine=findViewById(R.id.bottom_line);
        loadingResultContainer=findViewById(R.id.loading_result_container);
        topLine=findViewById(R.id.top_line);
        closeDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        findViewById(R.id.restart_download).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (shopId != -1){
                    download();
                }
            }
        });

    }
    ProgressBar progressBar;
    TextView percent;
    LinearLayout dialogContainer;
    TextView title;
    FrameLayout closeDialog;
    View topLine;
    TextView content;
    View bottomLine;
    RelativeLayout loadingResultContainer;

    private int shopId;

    DownloadManager manager;
    private String time;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress_loading_view);
        initFindView();
        init();
    }

    private void init() {
        shopId = getIntent().getIntExtra("shopId",-1);
        if (shopId != -1) {
            Date date = new Date();
            DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            time = format.format(date);
            Log.d(TAG, "init:tttttttttttttt "+time);
            manager = DownloadManager.getInstance();
            download();
        }
    }

    private void download(){
        loadingResultContainer.setVisibility(View.GONE);
        dialogContainer.setVisibility(View.VISIBLE);

        DownloadManager.downloadFile(ApiConfig.API_URL + "housekeeper/contract/export?shopId=" + shopId, "商户合同"+time, new DownloadManager.ResultCallback() {
            @Override
            public void onError(Request request, Exception e) {
                dialogContainer.setVisibility(View.GONE);
                loadingResultContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onResponse(Object response) {
                finish();
                Toast toast = Toast.makeText(ProgressDialogActivity.this, "下载完成，文件名为\n商户合同" + time + ".pdf", Toast.LENGTH_LONG);
                LayoutInflater inflater = LayoutInflater.from(ProgressDialogActivity.this);
                View inflate = inflater.inflate(R.layout.center_toast_view, null);
                TextView viewById = (TextView) inflate.findViewById(R.id.content);
                viewById.setText("下载完成，文件名为\n商户合同" + time + ".pdf");
                toast.setView(inflate);
                toast.setGravity(Gravity.CENTER,0,0);
                toast.show();
            }

            @Override
            public void onProgress(double total, double current) {
                Log.d(TAG, "onProgress: "+total);
                Log.d(TAG, "onProgress: "+current);
                int progress = (int) (100 * current / total);
                progressBar.setProgress(progress);
                percent.setText(progress+"%");

            }
        });
    }



}
